<?php
// no direct access
defined('_JEXEC') or die('Restricted access');

class modLkregistrationHelper {

	function getWmCategories() {
		// load vm-categories into an associative array
		$lang = JFactory::getLanguage();
		$lang = strtolower(str_replace("-","_",$lang->getTag()));
		$db = JFactory::getDBO();
		//dump($lang,'lang');
		$query = "
		SELECT l.virtuemart_category_id, l.category_name
		FROM #__virtuemart_categories_".$lang." l
		LEFT JOIN #__virtuemart_categories c
		ON l.virtuemart_category_id = c.virtuemart_category_id
		LEFT JOIN #__virtuemart_category_categories cc
		ON cc.id = c.virtuemart_category_id
		WHERE cc.category_parent_id = '0' 
		";
		//dumpMessage($query);
		$db->setQuery($query);
		//$cats = $db->loadResultArray();
		$cats = $db->loadAssocList('virtuemart_category_id', 'category_name');
		//dump($cats, 'cats-loadresularray');
		$tree = array();
		foreach ($cats as $k=>$c) {	$tree[]= array('id'=>$k,'name'=>$c);}
		//dump($tree, 'tree-loadresularray');
		return $tree;//array(array('id'=>1,'name'=>'O'),array('id'=>3,'name'=>'Elvis'));
	}
	function register($data) {
		//dump($data, 'data');
		$userData = array(
			'name'		=>	$data['jform']['name'],
			'username'	=>	$data['jform']['username'],
			'pass'		=>	self::encryptPass($data['jform']['password2']),
			'email'		=>	$data['jform']['email2'],
			'proj'		=>	$data['projection'],
			'params'	=> '{\"admin_style\":\"\",\"admin_language\":\"\",\"language\":\"\",\"editor\":\"\",\"helpsite\":\"\",\"timezone\":\"\"}'
		);
		//dump($userData, 'user-data');
		$userData['username'] = self::uniqueUsername($userData['username'], $count);
		if (self::uniqueEmail($userData['email'])){
			$db = JFactory::getDBO();
			$sql = "INSERT INTO #__users VALUES (" . 
			"'',".
			"'" . $userData['name'] . "',".
			"'" . $userData['username'] . "',".
			"'" . $userData['email'] . "',".
			"'" . $userData['pass'] . "',".
			"'',".
			"'0',".
			"'1',".
			"'" . date( 'Y-m-d H:i:s') . "',".
			"'" . date( 'Y-m-d H:i:s') . "',".
			"'',".
			"'" . $userData['params'] . "'".
			")";			
			$db->setQuery($sql);
			$db->query();
			$newID = $db->insertid();
			if ($db->getErrorNum()) {
				JError::raiseWarning(500, $db->stderr());
				return false;
			} else {
				$cats_sql = "INSERT INTO #__user_categories VALUES (" .
				"'',".
				"'" . $newID . "',".
				"'" . $userData['proj'] . "'".
				");";
				//dumpMessage($cats_sql);
				$db->setQuery($cats_sql);
				$db->query();
				
				// Add to group
				$group_sql = "INSERT INTO #__user_usergroup_map VALUES (".
				//"'',".
				"'" . $newID . "',".
				"'2'".
				");";
				//dumpMessage($group_sql);
				$db->setQuery($group_sql);
				$db->query();
				//var_dump($sql);
				if ($db -> getErrorNum()) {
					JError::raiseWarning(500, $db->stderr());
					return false;
				} else {
					return true;
				}
			}
		}else{
			JError::raiseWarning(500, "Email ya existe.");
			return false;
		}
	}
	function encryptPass($password){
		jimport('joomla.user.helper');
		$salt = JUserHelper::genRandomPassword(32);
		$crypt = JUserHelper::getCryptedPassword($password, $salt);
		return $crypt.':'.$salt;
	}	
	function uniqueUsername($username, $count){		
		$db = JFactory::getDBO();
		$query = "SELECT username" . " FROM #__users" . " WHERE username LIKE " . $db->Quote($username . "%");
		$db -> setQuery($query);
		$exists = $db->loadObjectList();
		$count = count($exists);
		if ($count != 0){$username = $username . ($count+1);}
		return $username;
	}
	function uniqueEmail($email){
		$db = JFactory::getDBO();
		$query = "SELECT email" . " FROM #__users" . " WHERE email = " . $db->Quote($email);
		$db -> setQuery($query);
		$exists = $db -> loadObjectList();
		$count = count($exists);
		if ($count != 0){
			return false;
		}else
			return true;
	}
	
}
?>